package com.ethink.framework.common.web;

import com.ethink.framework.common.constant.CommonRespCode;
import com.ethink.framework.common.constant.ResponseCodeEnum;
import com.ethink.framework.common.web.response.CommonResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ethink.framework.log.constant.LogConstant;
import org.slf4j.MDC;
import org.springframework.web.bind.annotation.CrossOrigin;

import java.util.List;

/**
 * controller 父类
 *
 * @Author name(Tel : mobile)
 * @Date 2020-06-06 23:15
 **/
@CrossOrigin
public class BaseController {

    /**
     * 返回成功响应
     *
     * @return
     */
    public CommonResponse succeed() {
        CommonResponse response = new CommonResponse();
        response.setCode(CommonRespCode.SUCCESS.getCode());
        response.setMessage(CommonRespCode.SUCCESS.getMessage());
        response.setTraceId(MDC.get(LogConstant.LOG_TRACE_ID));
        return response;
    }

    public CommonResponse failed() {
        CommonResponse response = new CommonResponse();
        response.setCode(CommonRespCode.SYSTEM_ERR.getCode());
        response.setMessage(CommonRespCode.SYSTEM_ERR.getMessage());
        response.setTraceId(MDC.get(LogConstant.LOG_TRACE_ID));
        return response;
    }

    public CommonResponse failedWith(ResponseCodeEnum responseCodeEnum) {
        CommonResponse response = new CommonResponse();
        response.setCode(responseCodeEnum.getCode());
        response.setMessage(responseCodeEnum.getMessage());
        response.setTraceId(MDC.get(LogConstant.LOG_TRACE_ID));
        return response;
    }

    public <T>  CommonResponse<T> failedWith(ResponseCodeEnum responseCodeEnum, T data) {
        CommonResponse response = new CommonResponse();
        response.setCode(responseCodeEnum.getCode());
        response.setMessage(responseCodeEnum.getMessage());
        response.setData(data);
        response.setTraceId(MDC.get(LogConstant.LOG_TRACE_ID));
        return response;
    }

    /**
     * 返回带数据的成功响应
     *
     * @param data
     * @param <T>
     * @return
     */
    public <T> CommonResponse<T> succeedWith(T data) {
        CommonResponse response = new CommonResponse();
        response.setCode(CommonRespCode.SUCCESS.getCode());
        response.setMessage(CommonRespCode.SUCCESS.getMessage());
        response.setData(data);
        response.setTraceId(MDC.get(LogConstant.LOG_TRACE_ID));
        return response;
    }

    /**
     * 返回分页查询结果(Page)
     *
     * @param page
     * @param <T>
     * @return
     */
    public <T> CommonResponse<List<T>> succeedWithPage(Page<T> page) {
        CommonResponse response = new CommonResponse<>();
        response.setCode(CommonRespCode.SUCCESS.getCode());
        response.setMessage(CommonRespCode.SUCCESS.getMessage());
        response.setData(page.getRecords());
        response.setTotal(page.getTotal());
        response.setTraceId(MDC.get(LogConstant.LOG_TRACE_ID));
        return response;
    }

    /**
     * 参数校验 返回参数和参数异常的原因
     * @param data
     * @param <T>
     * @return
     */
    public <T> CommonResponse<T> errorWithErrorParam(T data){
        CommonResponse response = new CommonResponse();
        response.setCode(CommonRespCode.PARAM_ERR.getCode());
        response.setMessage(CommonRespCode.PARAM_ERR.getMessage());
        response.setData(data);
        response.setTraceId(MDC.get(LogConstant.LOG_TRACE_ID));
        return response;
    }
}
